package edu.mtu.citizenscience.ethnography;

import edu.mtu.citizenscience.ethnography.uploader.EthnographyUploadService;
import edu.mtu.citizenscience.ethnography.util.LoginDialogFragment;
import edu.mtu.citizenscience.ethnography.util.Util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends FragmentActivity {

	@SuppressWarnings("unused")
	private static final String LOG_TAG = "SettingsActivity";
	
	private TextView loginStatusLabel;
	private TextView usernameLabel;
	
	private Button loginButton;
	private Button registerButton;
	private Button logoutButton;
	
	private BroadcastReceiver receiver;
	private IntentFilter intentFilter = new IntentFilter(EthnographyUploadService.UPLOAD_SERVICE);
	private LocalBroadcastManager manager;
	
	private SharedPreferences prefs;
	private SharedPreferences.OnSharedPreferenceChangeListener listener;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		manager = LocalBroadcastManager.getInstance(this);
		
		loginStatusLabel = (TextView) findViewById(R.id.loginStatusLabel);
		usernameLabel = (TextView) findViewById(R.id.usernameLabel);
		loginButton = (Button) findViewById(R.id.loginButton);
		registerButton = (Button) findViewById(R.id.registerButton);
		logoutButton = (Button) findViewById(R.id.logoutButton);
		
		prefs = getSharedPreferences(Util.PREFS_KEY, MODE_PRIVATE);
		listener = new OnSharedPreferenceChangeListener() {
			
			@Override
			public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
					String key) {
				boolean loggedIn = Util.isLoggedIn(getBaseContext());
				loginButton.setEnabled(!loggedIn);
				registerButton.setEnabled(!loggedIn);
				logoutButton.setEnabled(loggedIn);
				
				if (loggedIn) {
					loginStatusLabel.setText(R.string.loggedInAs);
					usernameLabel.setText(prefs.getString(Util.PREFS_USERNAME, null));
				} else {
					loginStatusLabel.setText(R.string.notLoggedIn);
					usernameLabel.setText("");
				}
			}
		};
		prefs.registerOnSharedPreferenceChangeListener(listener);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		String username = prefs.getString(Util.PREFS_USERNAME, null);
		
		boolean loggedIn = (username != null);
		
		loginButton.setEnabled(!loggedIn);
		registerButton.setEnabled(!loggedIn);
		logoutButton.setEnabled(loggedIn);
		
		if (loggedIn) {
			loginStatusLabel.setText(R.string.loggedInAs);
			usernameLabel.setText(username);
		} else {
			loginStatusLabel.setText(R.string.notLoggedIn);
			usernameLabel.setText("");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_settings, menu);
		return true;
	}
	
	public void doLogin(View view) {
		receiver = new LoginBroadcastReceiver();
		manager.registerReceiver(receiver, intentFilter);
		
		LoginDialogFragment fragment = new LoginDialogFragment();
		fragment.setCallingActivity(this);
		fragment.show(getSupportFragmentManager(), LoginDialogFragment.FRAG_TAG);
	}
	
	public void doRegister(View view) {
		Intent intent = new Intent(this, RegisterActivity.class);
		startActivity(intent);
	}
	
	public void doLogout(View view) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.remove(Util.PREFS_API_KEY);
		editor.remove(Util.PREFS_USERNAME);
		editor.commit();
	}
	
	private class LoginBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String result = intent.getStringExtra(EthnographyUploadService.OUTPUT_RESULT);

			Toast toast = Toast.makeText(context, Util.loginStringIDs.get(result), Toast.LENGTH_SHORT);
			toast.show();
			manager.unregisterReceiver(receiver);
			receiver = null;
		}

	}
}
